const axios = require('axios').default

/**
 * 1-请求拦截器，先添加，后执行；响应拦截器，相反
 * 2-拦截器需要在发请求前添加。
 */



// 添加请求拦截器（回调函数）
axios.interceptors.request.use(
  config => {
    console.log('request interceptor1 onResolved')
    return config
  },
  error => {
    console.log('request interceptor1 error onRejected')
    return Promise.reject(error)
  }
)

axios.interceptors.request.use(
  config => {
    console.log('request interceptor2 onResolved')
    return config
  },
  error => {
    console.log('request interceptor2 error onRejected')
    return Promise.reject(error)
  }
)

// 添加响应拦截器（回调函数）
axios.interceptors.response.use(
  response => {
    console.log('response interceptor1 onResolved');
    return response
  },
  error => {
    console.log('response interceptor1 onRejected');
    return Promise.reject(error)
  }
)

axios.interceptors.response.use(
  response => {
    console.log('response interceptor2 onResolved');
    return response
  },
  error => {
    console.log('response interceptor2 onRejected');
    return Promise.reject(error)
  }
)

axios.get('http://localhost:3000/posts')
.then(res => {
  console.log(res.data);
})
.catch(err => {
  console.log(err);
})

